from PySide6.QtWidgets import QDialog,QMessageBox
from sqlalchemy.orm import Session
from PySide6.QtCore import Qt,QDate
import datetime,uuid,pandas as pd
from settings.publicvar import engine
from views.person.model.person import PersonBaseInfo,PersonType,SurePersonType
from views.bankinfo.model.bankinfo import BankCard
from views.issuedmange.model.issuedmange import IssuedPersonSure
from views.initsetting.mode.initsetting import InitSetting
from views.initsetting.initsettingView import InitSettingView
from views.issuedmange.sureissuedperson.operation import Ui_operation

class OperationView(QDialog,Ui_operation):
    def __init__(self,data):
        super().__init__()
        self.setupUi(self)
        self.returndata = []
        self.autoData(data)
        self.automenuData()
        self.addbtn.clicked.connect(self.addFun)

    def autoData(self,data):
        for item in data:
            self.name.addItem(str(item.id) + '-' + item.name)

    def addFun(self):
        school = self.school.currentText()
        persontype = self.persontype.currentText()
        issuedtype = self.issuedtype.currentText()
        issueddate = self.issueddate.date()
        name_id = self.name.currentText().split('-')[0]
        with Session(engine) as session, session.begin():
            personinfo = session.query(PersonBaseInfo).filter(PersonBaseInfo.id == name_id).all()
            issueddate = datetime.date(issueddate.year(), issueddate.month(), issueddate.day())
            tem = []
            IDcards = []
            for item in personinfo:
                IDcards.append(item.IDcard)
                uniqueID = str(uuid.uuid4()) + '-' + str(datetime.datetime.now())
                tem.append({'school': school, 'name': item.name, 'IDcard': item.IDcard,
                            'paymenttype': issuedtype, 'persontype': persontype, 'issueddate': issueddate,
                            'uniqueID': uniqueID})
            bankcards = session.query(BankCard).filter(BankCard.IDNumber.in_(IDcards)).all()
            bankcards = [{'IDcard': item.IDNumber, 'bankName': item.bankname, 'cardNumber': item.cardnumber,
                          'openbank': item.openbank} for item in bankcards]
            surepersons = pd.DataFrame(tem)
            bankcards = pd.DataFrame(bankcards)
            surepersontypes = pd.merge(surepersons, bankcards, on='IDcard')
            surepersontypes['mark'] = ''
            surepersontypes = surepersontypes.to_dict('records')
            self.returndata = [IssuedPersonSure(**item) for item in surepersontypes]
        self.accept()

    # 导航数据加载
    def automenuData(self):
        with Session(engine) as session, session.begin():
            self.issueddate.setDate(QDate.currentDate())
            try:
                initseting = session.query(InitSetting).one()
                name = initseting.school.name
                self.school.addItem(name)
                self.school.setCurrentText(name)
                persontypes = session.query(PersonType).all()
                for item in persontypes:
                    self.persontype.addItem(item.persontype)
                self.persontype.setCurrentText('保育员')
            except:
                QMessageBox.critical(None,'失败','请在设置里初始化设置，才能操作！')
                InitSettingView().exec()